# -*- coding: utf-8 -*-
"""
Created on Mon Dec 23 16:43:25 2019

@author: MS
"""

from sklearn.model_selection import GridSearchCV
from sklearn import svm,neural_network,tree
from sklearn import datasets
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler  

'''
=============载入数据集iris=======================
'''
X,y=datasets.load_iris(return_X_y=True)

normalize=0   #设为1则作归一化，否则不归一化
if normalize==1:
    X=StandardScaler().fit(X).transform(X) #对数据进行归一化

#===========1.svm-linear=======
C=np.logspace(-2,5,20)
clf1=GridSearchCV(svm.SVC(kernel='linear'),{'C':C},cv=5,return_train_score=True)
clf1.fit(X,y)

print('svm-linear-最佳参数：',clf1.best_params_,
      '最佳得分:',clf1.best_score_,
      '对应拟合耗时:',clf1.cv_results_['mean_fit_time'][clf1.best_index_])

#===========2.svm-rbf==========
clf2=GridSearchCV(svm.SVC(kernel='rbf'),{'C':C},cv=5,return_train_score=True)
clf2.fit(X,y)

print('svm-rbf-最佳参数：',clf2.best_params_,
      '最佳得分:',clf2.best_score_,
      '对应拟合耗时:',clf2.cv_results_['mean_fit_time'][clf2.best_index_])

#===========3.BP神经网络================
alpha=[0]+list(np.logspace(-8,1,10))
nodes=[(i,) for i in range(5,30,10)]+\
      [(i,j) for i in range(5,30,10) for j in range(2,10,5)]
clf3=GridSearchCV(neural_network.MLPClassifier(solver='lbfgs'),
                  {'alpha':alpha,'hidden_layer_sizes':nodes},cv=5,return_train_score=True)

clf3.fit(X,y)
print('BP-最佳参数：',clf3.best_params_,
      '最佳得分:',clf3.best_score_,
      '对应拟合耗时:',clf3.cv_results_['mean_fit_time'][clf3.best_index_])

#===========4.决策树================
deep=[None,2,4,8,10,12,14,16]
minleaf=[5,4,3,2,1]
clf4=GridSearchCV(tree.DecisionTreeClassifier(),
                  {'max_depth':deep,'min_samples_leaf':minleaf},cv=5,return_train_score=True)
clf4.fit(X,y)
print('Tree-最佳参数：',clf4.best_params_,
      '最佳得分:',clf4.best_score_,
      '对应拟合耗时:',clf4.cv_results_['mean_fit_time'][clf4.best_index_])